package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.dto.RefreshTokenDto;
import cn.itsource.ymcc.vo.LoginVo;
import cn.itsource.ymcc.domain.Login;
import cn.itsource.ymcc.dto.LoginCommonDto;
import cn.itsource.ymcc.exception.GlobalException;
import cn.itsource.ymcc.mapper.LoginMapper;
import cn.itsource.ymcc.service.ILoginService;
import cn.itsource.ymcc.util.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author gwk
 * @since 2023-02-19
 */
@Service
@Slf4j
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {


    @Override
    public List<String> getAuthorByLoginID(Long id) {

        return baseMapper.getAuthorByLoginID(id);
    }

    @Override
    public LoginVo loginCommon(LoginCommonDto login) {
        // 1. jsr303参数校验
        String type = login.getType();
        String password = login.getPassword();
        String username = login.getUsername();

        String url = "http://localhost:10020/oauth/token?client_id=%s&client_secret=%s&grant_type=password&username=%s&password=%s";

        String client_id = "";
        String client_secret = "";
        //2. 根据类型判断是前台还是后台
        if (type.equals("0")) {
            // 后台
            client_id = "backstage";
            client_secret = "1";
        }else if(type.equals("1")){
            // 前端
            client_id = "website";
            client_secret = "1";
        }
        //3.发送请求

        url = String.format(url, client_id, client_secret, username, password);

        log.info("url:{}",url);
        String jsonResult = HttpUtil.sendPost(url, null);
        log.info("jsonResult:{}",jsonResult);

        if(jsonResult.contains("error")){
            throw new GlobalException("登录失败！");
        }

        JSONObject jsonObject = JSON.parseObject(jsonResult, JSONObject.class);
        LoginVo loginVo = new LoginVo();
        loginVo.setAccess_token(jsonObject.getString("access_token"));
        loginVo.setRefresh_token(jsonObject.getString("refresh_token"));
        Long expires_in = jsonObject.getLong("expires_in");

        loginVo.setExpiresTime(expires_in+System.currentTimeMillis());

        return loginVo;
    }

    @Override
    public LoginVo refreshToken(RefreshTokenDto refreshTokenDto) {
        // 通过用户名判断 前端还是后台用户
        String username = refreshTokenDto.getUsername();
        String refreshToken = refreshTokenDto.getRefreshToken();

        Wrapper<Login> wrapper = new EntityWrapper<>();
        wrapper.eq("username",username);
        Login login = selectOne(wrapper);

        String url ="http://localhost:10020/oauth/token?grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s";


        String client_id = "";
        String client_secret = "";
        //2. 根据类型判断是前台还是后台
        if (login.getType() == 0) {
            // 后台
            client_id = "backstage";
            client_secret = "1";
        }else if(login.getType() == 1){
            // 前端
            client_id = "website";
            client_secret = "1";
        }
        //3.发送请求

        url = String.format(url, refreshToken, client_id, client_secret);

        log.info("url:{}",url);
        String jsonResult = HttpUtil.sendPost(url, null);
        log.info("jsonResult:{}",jsonResult);

        if(jsonResult.contains("error")){
            throw new GlobalException("获取刷新token失败！");
        }

        JSONObject jsonObject = JSON.parseObject(jsonResult, JSONObject.class);
        LoginVo loginVo = new LoginVo();
        loginVo.setAccess_token(jsonObject.getString("access_token"));
        loginVo.setRefresh_token(jsonObject.getString("refresh_token"));
        Long expires_in = jsonObject.getLong("expires_in");

        loginVo.setExpiresTime(expires_in+System.currentTimeMillis());

        return loginVo;
    }
}
